using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Game1
{
    public struct mousesquare
    {
        public float xa;
        public float xb;
        public float ya;
        public float yb;
    }
    public class MenuEntry
    {
        private string Name;
        protected string Caption;
        private Vector2 position;
        private mousesquare mousesquare;
        public float scale;
        public float mousefontscalex;
        public float mousefontscaley;
        Color color;

        public string shortcut;
        public string action;
        public string param;

        public MenuEntry(string Name, string Caption, int X, int Y, float scale, Color color, float dx, float dy)
        {
            this.Name = Name;
            this.Caption = Caption;
            this.position.X = (float)X;
            this.position.Y = (float)Y;
            this.scale = scale;
            this.color = color;
            mousefontscalex = 1;
            mousefontscaley = 1;
            shortcut = null;
            action = null;
            param = null;
            setMouseSquare(dx, dy);
        }

        public string getName()
        {
            return Name;
        }

        public string getCaption()
        {
            return Caption;
        }

        public void changeCaption(string newCaption)
        {
            Caption = newCaption;
        }

        public Vector2 getPosition()
        {
            return position;
        }

        public Color getColor()
        {
            return color;
        }

        public void changeColor(Color color)
        {
            this.color = color;
        }
        public void setMouseSquare(float deltax, float deltay)
        {
            this.mousesquare.xa = this.getPosition().X;
            this.mousesquare.xb = this.getPosition().X + mousefontscalex * deltax;
            this.mousesquare.ya = this.getPosition().Y;
            this.mousesquare.yb = this.getPosition().Y + mousefontscaley * deltay;
        }
        public mousesquare getMouseSquare()
        {
            return this.mousesquare;
        }
        public bool verifyMouseInSquare()
        {
            if (Input.getMousePosition().X > mousesquare.xa &&
                Input.getMousePosition().X < mousesquare.xb &&
                Input.getMousePosition().Y > mousesquare.ya &&
                Input.getMousePosition().Y < mousesquare.yb)
            {
                return true;
            }
            return false;
        }

    }

    public class MenuEntryTextBox : MenuEntry
    {
        private string text = "";
        private int show = 1;
        private KeyboardState curState;
        private KeyboardState lastState;
        private float time = 400;
        private int charLimit;

        public bool active;

        Keys[] pressedKeys = null;
        Keys[] lastPressedKeys = null;

        public MenuEntryTextBox(string Name, int X, int Y, Color color,int charLimit)
            : base(Name,"",X,Y,1.0f,color,30,25)
        {
            lastState = Keyboard.GetState();
            active = true;
            this.charLimit = charLimit;
        }

        public string getTypedText()
        {
            if (!active) return text;
            else return null;
        }

        public void setClear()
        {
            text = "";
        }

        public void update(GameTime gameTime)
        {

            if (active)
            {
                pressedKeys = Keyboard.GetState().GetPressedKeys();
                if (lastPressedKeys == null) lastPressedKeys = Keyboard.GetState().GetPressedKeys();

                foreach (Keys key in pressedKeys)
                {
                    bool ok = true;
                    bool upcase = false;

                    //  if (pressedKeys.Length == 0) time = 3000;

                    foreach (Keys oldkey in lastPressedKeys)
                    {
                        if (oldkey == key) ok = false;
                        if (oldkey == Keys.LeftShift || oldkey == Keys.RightShift) upcase = true;
                    }

                    if (ok || time <= 0)
                    {                        
                        if (text.Length<=charLimit)
                        {
                            if (key.ToString().Length == 1 && upcase) text += key.ToString();
                            else if (key.ToString().Length == 1 && !upcase) text += key.ToString().ToLower();
                            else if (key.ToString().Length == 2) text += key.ToString().Substring(1, 1);
                            else if (key.ToString().StartsWith("NumPad")) text += key.ToString().Substring(6, 1);
                            else if (key == Keys.OemComma) text += ",";
                            else if (key == Keys.OemPeriod) text += ".";
                            else if (key == Keys.OemSemicolon) text += ";";
                            else if (key == Keys.OemMinus && upcase) text += "_";
                            else if (key == Keys.OemMinus && !upcase) text += "-";
                            else if (key == Keys.OemPlus) text += "+";
                            else if (key == Keys.Space) text += " "; 
                        }
                        if (key == Keys.Back && text.Length > 0) text = text.Substring(0, text.Length - 1);
                        else if (key == Keys.Enter) active = false;
                        show = 1;

                    }
                    else
                    {
                        time -= gameTime.ElapsedGameTime.Milliseconds;
                    }

                    //text = key.ToString();
                }

                if (pressedKeys.Length == 0)
                {
                    time = 400;
                }

                for (int i = 0; i < pressedKeys.Length && i < lastPressedKeys.Length; i++)
                {
                    if (pressedKeys[i] != lastPressedKeys[i]) time = 400;
                }

                lastPressedKeys = pressedKeys;

                if (gameTime.TotalGameTime.Milliseconds % 700 == 0) show *= -1;

                if (show == 1) Caption = text + "|";
                else Caption = text;
            }
            else Caption = text;
        }

    }
}
